package ua.edu.nuos.tees.jdmmon.gui.dialogs;

import ua.edu.nuos.tees.jdmmon.bl.MultimeterService;
import ua.edu.nuos.tees.jdmmon.dao.implementations.Rs232Dao;
import utils.ConfigUtils;
import vo.Config;
import vo.PATHS;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Properties;

/**
 * Dialog for menu item Settings
 *
 * @author Olha Mudrievska
 *         22.05.13
 */
public class SettingsDialog extends JDialog {

    private JPanel contentPane = new JPanel();
    private JButton buttonOK;
    private JButton buttonCancel;
    private JLabel slaveIdlabel;
    private JLabel functionLabel;
    private JTextField slaveIdTextField;
    private JComboBox functionComboBox;
    private Config config;
    private Rs232Dao dao;

    public SettingsDialog(final boolean state) {

        slaveIdlabel = new JLabel("Slave ID: ");
        slaveIdTextField = new JTextField(5);
        functionLabel = new JLabel("Function: ");
        String function[] = {"03 Holding Register (4x)"};
        functionComboBox = new JComboBox(function);
        buttonOK = new JButton("OK");
        buttonCancel = new JButton("CANCEL");

        setContentPane(contentPane);
        contentPane.setLayout(new GridBagLayout());
        setModal(true);

        addComponent(slaveIdlabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 0);
        addComponent(slaveIdTextField, contentPane, GridBagConstraints.WEST, 2, 1, 0.5, 0.5, 1, 0);
        addComponent(functionLabel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 0.5, 0, 1);
        addComponent(functionComboBox, contentPane, GridBagConstraints.WEST, 2, 1, 0.5, 0.5, 1, 1);
        addComponent(buttonOK, contentPane, GridBagConstraints.EAST, 1, 1, 0.5, 1.0, 1, 2);
        addComponent(buttonCancel, contentPane, GridBagConstraints.CENTER, 1, 1, 0.5, 1.0, 2, 2);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if (state) {
                        dao.setDeviceAddress(Integer.parseInt(slaveIdTextField.getText()));
                    }
                    config = new Config(new Properties(), PATHS.MODBUS_PROPERTIES);
                    config.appendProperty("MODBUS_READ_INPUT_REGISTER_FUNCTION", "3");
                    config.appendProperty("DATA_SIZE", "73");
                    config.appendProperty("DEVICE_ADDRESS", slaveIdTextField.getText());
                    ConfigUtils.storeConfig(config);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                dispose();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        /**
         *  Exit on ESCAPE
         */
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    public void setDao(MultimeterService multimeterService) {
        dao = (Rs232Dao) multimeterService.getDAO();
    }

    /**
     * Add component to container
     *
     * @param anchor     the initial anchor value
     * @param gridwidth  the initial gridwidth value
     * @param gridheight the initial gridheight value
     * @param weightx    the initial weightx value
     * @param weighty    the initial weighty value
     * @param gridx      the initial gridx value
     * @param gridy      the initial gridy value
     */
    private void addComponent(Component component, Container pane, int anchor, int gridwidth, int gridheight,
                              double weightx, double weighty, int gridx, int gridy) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = anchor;
        gridBagConstraints.gridwidth = gridwidth;
        gridBagConstraints.gridheight = gridheight;
        gridBagConstraints.weightx = weightx;
        gridBagConstraints.weighty = weighty;
        gridBagConstraints.gridx = gridx;
        gridBagConstraints.gridy = gridy;
        pane.add(component, gridBagConstraints);
    }
}

